home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / Car / Road.m < prev    next >
Encoding:
Text File  |  1992-06-25  |  2.5 KB  |  128 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Car_main.h"
  5. #import "Road.h"
  6. #import "DataView.h"
  7. #import <streams/streams.h>
  8. #import <appkit/nextstd.h>
  9.  
  10. @implementation Road
  11.  
  12. - init
  13. {
  14.     [super init];
  15.     road = self;
  16.  
  17.     data = [[Storage alloc] initCount:0
  18.                     elementSize:sizeof(struct plotData)
  19.                             description:PLOTDATA_DESCRIPTION];
  20.     return self;
  21. }
  22.  
  23. - data
  24. {
  25.     return data;
  26. }
  27.  
  28. - (float)temperature
  29. {
  30.     return temperature;
  31. }
  32.  
  33. - setTemperature:(float)aNumber
  34. {
  35.     temperature = aNumber;
  36.     return self;
  37. }
  38.  
  39. - (float)windSpeed
  40. {
  41.     return windSpeed;
  42. }
  43.  
  44. - setWindSpeed:(float)aNumber
  45. {
  46.     windSpeed = aNumber;
  47.     return self;
  48. }
  49.  
  50. - loadRoadFromStream:(NXStream *)stream
  51. {
  52. struct plotData point;
  53. float length;
  54. float slope;
  55.  
  56.     [data empty];
  57.     point.x = 0;
  58.     point.y = 0;
  59.     [data addElement:&point];
  60.  
  61.     NXScanf(stream,"%f\n%f\n",&temperature,&windSpeed);
  62.     while ( !NXAtEOS(stream) )
  63.         {
  64.         NXScanf(stream,"%f,%f\n",&length,&slope);
  65.         point.x = point.x + length;
  66.         point.y = point.y + slope * length;
  67.         [data addElement:&point];
  68.         }
  69.     return self;
  70. }
  71.  
  72. - (float)slope
  73. {
  74. float slope;
  75. struct plotData *points;
  76. int count;
  77. int after;
  78. int before;
  79.  
  80.     points = (struct plotData *)data->dataPtr;
  81.     count = [data count];
  82.  
  83.     if ( count == 0 )                            // If there is no road file loaded, return 0 slope
  84.         return 0;
  85.  
  86.     if ( currentPosition > points[count-1].x )                // If there is no more data, return 0 slope
  87.         return 0;
  88.     
  89.     for ( after = 0 ; points[after].x < currentPosition ; after++ );    // Look for a point that is >= the current position.
  90.    
  91.     if ( points[after].x == currentPosition )                // We want a middle derivative.
  92.         after = after++;
  93.     after = MIN(after,count-1);                        // Check to see that we don't go beyond the data.
  94.  
  95.     for ( before = after - 1 ; before >= 0 && points[before].x >= currentPosition ; before-- );
  96.     before = MAX(before,0);                        // Check to see that we don't go beyond the data.
  97.  
  98.     slope = ( points[after].y - points[before].y ) / ( points[after].x - points[before].x );
  99.  
  100.     return slope;
  101. }
  102.  
  103. - reset
  104. {
  105.     currentPosition = 0;
  106.     return self;
  107. }
  108.  
  109. - advance:(float)time atSpeed:(float)velocity
  110. {
  111.     currentPosition += time * velocity;
  112.     return self;
  113. }
  114.  
  115. - (float)distance
  116. {
  117.     return currentPosition;
  118. }
  119.  
  120. - report:(NXStream *)stream
  121. {
  122.     NXPrintf(stream,"Environment:\n");
  123.     NXPrintf(stream,"  Distance Travelled:%fkm\n",currentPosition/1000);
  124.     return self;
  125. }
  126.  
  127. @end
  128.